
using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;

namespace Widgetsphere.Core.DataAccess
{
	/// <summary>
	/// Summary description for a database stored procedure.
	/// </summary>
	public partial class StoredProcedure
	{
		#region Class Members

		private string _storedProcedureName;
		private List<IDbDataParameter> _dataSetParameters;
		private List<IDbDataParameter> _directQueryParameters;
		private DateTime _lastAccessed;

		#endregion

		#region Constructors

		internal StoredProcedure(string connectionString, string storedProcedureName)
		{
			_storedProcedureName = storedProcedureName;
			_dataSetParameters = ParameterHelper.GetInstance().GetDataSetParameterArray(connectionString, storedProcedureName);
			_directQueryParameters = ParameterHelper.GetInstance().GetDirectQueryParameterCollection(connectionString, storedProcedureName);
			_lastAccessed = DateTime.Now;
		}

		#endregion

		#region Methods

		internal IDbCommand GetDataSetCommand()
		{
			return GetCommand(true);
		}

		internal IDbCommand GetDirectQueryCommand()
		{
			return GetCommand(false);
		}

		private IDbCommand GetCommand(bool dataSetCommand)
		{
			IDbCommand returnCommand = DatabaseHelper.GetCommand();
			returnCommand.CommandText = _storedProcedureName;
			returnCommand.Parameters.Clear();
			if (dataSetCommand)
			{
				foreach (IDbDataParameter param in _dataSetParameters)
				{
					returnCommand.Parameters.Add(ParameterHelper.GetInstance().GetParameterClone(param));
				}
			}
			else
			{
				foreach (IDbDataParameter param in _directQueryParameters)
				{
					returnCommand.Parameters.Add(ParameterHelper.GetInstance().GetParameterClone(param));
				}
			}
			_lastAccessed = DateTime.Now;
			return returnCommand;
		}
		
		#endregion

	}
}
